package com.example.Sao.MyFragment;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v7.widget.CardView;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import com.example.Sao.R;
import com.example.Sao.Tools.DesECBUtil;

import cn.pedant.SweetAlert.SweetAlertDialog;

/**
 * @ProjectName: scan
 * @Package: com.example.Sao.MyFragment
 * @ClassName: Logn
 * @Description: java类作用描述
 * @Author: 作者名
 * @CreateDate: 2020/6/1 21:27
 * @UpdateUser: 更新者：
 * @UpdateDate: 2020/6/1 21:27
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */
public class LogonDialog extends DialogFragment implements View.OnClickListener {
    private Context mContext;
    private EditText input_password;
    private CardView logon;
    private SweetAlertDialog sweetAlertDialog;
   private onLogonStaute logonStaute;

    public void setLogonStaute(onLogonStaute logonStaute) {
        this.logonStaute = logonStaute;
    }

    public void setmContext(Context mContext) {
        this.mContext = mContext;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Window window = this.getDialog().getWindow();
        setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
        //去掉dialog默认的padding
        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        window.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = (int) (dm.widthPixels * 0.75);
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        //设置dialog的位置在底部
        lp.gravity = Gravity.CENTER;
        //设置dialog的动画
        lp.windowAnimations = R.style.dialogAnim;
        window.setAttributes(lp);
        window.setBackgroundDrawable(new ColorDrawable());
        getDialog().setCanceledOnTouchOutside(false);//点击屏蔽不消失
        View view = inflater.inflate(R.layout.fragment_logon_layout,container,false);
        input_password = view.findViewById(R.id.password);
        logon = view.findViewById(R.id.logon);
        logon.setOnClickListener(this);
        return view;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.logon:
                String paww = DesECBUtil.encryptDES(input_password.getText().toString().getBytes(),"key_123_").replaceAll("\r|\n", "");
                if ("SSRvsdAwHU/oK1yCjEqODA==".equals(paww)){
                    if (logonStaute!=null){
                        logonStaute.onLogonStauteBack(1);
                    }
                    sweetAlertDialog = new SweetAlertDialog(mContext,SweetAlertDialog.SUCCESS_TYPE);
                    sweetAlertDialog.setTitleText("提示");
                    sweetAlertDialog.setContentText("登录成功，欢迎使用");
                    sweetAlertDialog.setConfirmText("成功");
                    sweetAlertDialog.setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                        @Override
                        public void onClick(SweetAlertDialog sweetAlertDialog) {
                            sweetAlertDialog.dismiss();
                            LogonDialog.this.dismiss();
                        }
                    });
                    sweetAlertDialog.show();
                }else {
                    if (logonStaute!=null){
                        logonStaute.onLogonStauteBack(0);
                    }
                    if (sweetAlertDialog==null) {
                        sweetAlertDialog = new SweetAlertDialog(mContext,SweetAlertDialog.WARNING_TYPE);
                    }
                    sweetAlertDialog.setTitleText("提示");
                    sweetAlertDialog.setContentText("密码错误，请重新输入");
                    sweetAlertDialog.setConfirmText("返回");
                    sweetAlertDialog.setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                        @Override
                        public void onClick(SweetAlertDialog sweetAlertDialog) {
                            sweetAlertDialog.dismiss();
                        }
                    });
                    sweetAlertDialog.show();
                }
//                sweetAlertDialog.
                break;
                default:
                    break;
        }
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        return super.onCreateDialog(savedInstanceState);
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onStart() {
        super.onStart();
        getDialog().getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
        getDialog().getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
            @Override
            public void onSystemUiVisibilityChange(int visibility) {
                int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
                        //布局位于状态栏下方
                        View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
                        //全屏
                        View.SYSTEM_UI_FLAG_FULLSCREEN |
                        //隐藏导航栏
                        View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
                        View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
                if (Build.VERSION.SDK_INT >= 19) {
                    uiOptions |= 0x00001000;
                } else {
                    uiOptions |= View.SYSTEM_UI_FLAG_LOW_PROFILE;
                }
                getDialog().getWindow().getDecorView().setSystemUiVisibility(uiOptions);
            }
        });
    }

    public interface onLogonStaute{
            public void onLogonStauteBack(int staute);
    }
}
